# -*- coding:utf-8 -*-
"""
@author: ksf

@since: 2020-04-25 21:29
"""

from enum import Enum


class HandlerLevel(Enum):
    """在事件主处理方法执行前后的哪一层注册这个事件"""
    FIRST = 100
    BEFORE_MAIN_CHECK = 300
    BEFORE_MAIN_HANDLER = 400
    ON_MAIN_HANDLER = 500
    AFTER_MAIN_HANDLER = 600
    END = 900


class HandlerCategory(Enum):
    EVENT = '特定处理'
    GENERAL = '全局处理'
    ORDER_CHECKER = '订单审查'


class WhenHandlerError(Enum):
    """在事件主处理方法执行前后的哪一层注册这个事件"""
    IGNORE = '忽略'
    LOG = '日志'
    RAISE = '抛出'


class RunningMod(Enum):
    BACKTEST = '回测'
    REAL = '实盘'


class DataLevel(Enum):
    tick = 100
    minute = 200
    hour = 300
    day = 400

    def next(self):
        try:
            return self.__next__()
        except StopIteration:
            return None

    def __next__(self):
        members = list(self.__class__)
        next_index = members.index(self) + 1
        if next_index >= len(members):
            raise StopIteration
        return members[next_index]


class OrderType(Enum):
    LMT = '限价单'
    MKT = '市价单'
    STOP = "STOP"
    FAK = "FAK"
    FOK = "FOK"
    RFQ = "询价"

class Direction(Enum):
    """
    Direction of order/trade/position.
    """
    LONG = "多"
    SHORT = "空"


class OptionType(Enum):
    CALL = 'CALL'
    PUT = 'PUT'

class Offset(Enum):
    """
    Offset of order/trade.
    """
    OPEN = "开仓"
    CLOSE = "平仓"
    CLOSE_TODAY = "平今"
    CLOSE_YESTERDAY = "平昨"


class OrderStatus(Enum):
    NEW = '新单'
    SUBMIT = '终端提交'
    ACCEPT = '已经受理'
    QUEUED = '已排队'
    PART_TRADED = '部分成交'
    ALL_TRADED = '全部成交'
    CANCELING = '排队撤单'
    ALL_CANCELLED = '全部撤单'
    LEFT_CANCELLED = '部分撤单'
    REJECTED = '拒绝'
    FAIL = '失败'


class Currency(Enum):
    USD = 'USD'
    HKD = 'HKD'
    CNH = 'CNH'
    CNY = 'CNY'


class Exchange(Enum):
    """
    Exchange.
    """
    # China
    CFFEX = "CFFEX"             # 中国金融期货交易所
    SHSE = "SHSE"               # 上海股票交易所
    SHFE = "SHSE"               # 上海股票交易所
    SZSE = "SZSE"               # 深圳股票交易所
    CZCE = "CZCE"           # 郑州期货交易所
    DCE = "DCE"             # 大连期货交易所
    INE = "INE"             # 上海国际能源交易所

    SGE = "SGE"             # 上海黄金交易所
    WXE = "WXE"             # 无锡钢铁交易所
    HKFE = "HKFE"           # Hong Kong Futures Exchange
    HKSE = "HKSE"           # Hong Kong Stock Exchange

    # Global
    SMART = "SMART"         # Smart Router for US stocks
    NYSE = "NYSE"           # New York Stock Exchnage
    NASDAQ = "NASDAQ"       # Nasdaq Exchange
    ARCA = "ARCA"           # ARCA Exchange
    EDGEA = "EDGEA"         # Direct Edge Exchange
    ISLAND = "ISLAND"       # Nasdaq Island ECN
    BATS = "BATS"           # Bats Global Markets
    IEX = "IEX"             # The Investors Exchange
    NYMEX = "NYMEX"         # New York Mercantile Exchange
    COMEX = "COMEX"         # COMEX of CME
    GLOBEX = "GLOBEX"       # Globex of CME
    IDEALPRO = "IDEALPRO"   # Forex ECN of Interactive Brokers
    CME = "CME"             # Chicago Mercantile Exchange
    ICE = "ICE"             # Intercontinental Exchange

    SGX = "SGX"             # Singapore Global Exchange
    CBOT = "CBT"            # Chicago Board of Trade
    CBOE = "CBOE"           # Chicago Board Options Exchange
    CFE = "CFE"             # CBOE Futures Exchange
    DME = "DME"             # Dubai Mercantile Exchange
    EUREX = "EUX"           # Eurex Exchange
    APEX = "APEX"           # Asia Pacific Exchange
    LME = "LME"             # London Metal Exchange
    BMD = "BMD"             # Bursa Malaysia Derivatives
    TOCOM = "TOCOM"         # Tokyo Commodity Exchange
    EUNX = "EUNX"           # Euronext Exchange
    KRX = "KRX"             # Korean Exchange
    OTC = "OTC"             # OTC Product (Forex/CFD/Pink Sheet Equity)
    IBKRATS = "IBKRATS"     # Paper Trading Exchange of IB

    # CryptoCurrency
    BITMEX = "BITMEX"
    OKEX = "OKEX"
    HUOBI = "HUOBI"
    BITFINEX = "BITFINEX"
    BINANCE = "BINANCE"
    BYBIT = "BYBIT"
    COINBASE = "COINBASE"
    DERIBIT = "DERIBIT"
    GATEIO = "GATEIO"
    BITSTAMP = "BITSTAMP"
    DYDX = "DYDX"
    FTX = "FTX"

    # Special Function
    LOCAL = "LOCAL"         # For local generated data
    BASKET = "BASKET"


class ContractType(Enum):
    """
    Product class.
    """
    STK = "股票"
    FUT = "期货"
    OPT = "期权"
    INX = "指数"
    FOR = "外汇"
    SPOT = "现货"
    ETF = "ETF"
    BOND = "债券"
    WARRANT = "权证"
    SPREAD = "价差"
    FUND = "基金"


DT_DATE_FORMAT = '%Y-%m-%d'
DT_DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'
DT_TIME_FORMAT = '%H:%M:%S'
